<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>全文搜索 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_full_text_search.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_full_text_search.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_full_text_search.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_full_text_search.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="intro.html">你知道的, 为了搜索…​</a></span>
»
<span class="breadcrumb-node">全文搜索</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_more_complicated_searches.html">« 更复杂的搜索</a>
</span>
<span class="next">
<a href="_phrase_search.html">短语搜索 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_full_text_search"></a>全文搜索<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/010_Intro/30_Tutorial_Search.asciidoc">edit</a>
</h2>
</div></div></div>
<p>到目前为止，搜索都很简单：单个姓名，通过年龄过滤。现在尝试下稍微高级点儿的全文搜索 —— 一项传统数据库确实很难搞定的任务。</p>
<p>搜索下所有喜欢攀岩（rock climbing）的员工：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/30_Query_DSL.json"></div>
<p>显然我们依旧使用之前的 <code class="literal">match</code> 查询在<code class="word">about</code>字段上搜索“rock climbing”。得到两个匹配的文档：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
   ...
   "hits": {
      "total":      2,
      "max_score":  0.16273327,
      "hits": [
         {
            ...
            "_score":         0.16273327, <a id="CO5-1"></a><i class="conum" data-value="1"></i>
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_score":         0.016878016, <a id="CO5-2"></a><i class="conum" data-value="1"></i>
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO5-1"><i class="conum" data-value="1"></i></a><a href="#CO5-2"></a></p>
</td>
<td align="left" valign="top">
<p>相关性得分(relevance scores)</p>
</td>
</tr>
</table>
</div>
<p>Elasticsearch 默认按照相关性得分排序，即按照每个文档与查询的匹配程度进行排序。第一个最高得分的结果很明显：John Smith 的 <code class="word">about</code> 字段清楚地写着 “rock climbing” 。</p>

<p>但为什么 Jane Smith 也作为结果返回了呢？原因是她的 <code class="word">about</code> 字段里提到了“rock” 。因为只有“rock”而没有“climbing”，所以她的 <code class="word">_score</code>(相关性得分) 低于 John 的。</p>

<p>这是一个很好的案例，阐明了 Elasticsearch 如何<em>在 (within)</em>全文字段上搜索并首先返回相关性最强的结果。Elasticsearch中的<em>相关性(relevance)</em>概念非常重要，也是完全区别于传统关系型数据库的一个概念，传统关系型数据库中的一条记录要么匹配要么不匹配。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="_more_complicated_searches.html">« 更复杂的搜索</a>
</span>
<span class="next">
<a href="_phrase_search.html">短语搜索 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>